package com.lfcsoft.client.listener;

import com.lfcsoft.client.consts.RabbitConstant;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liqinwen
 * @date 2021-3-4 15:12
 */
@Component
public class RabbitMsgListener {
    @RabbitListener(queues = RabbitConstant.ORDER_QUEUE)
    public void oderQueueListener(Message message, Channel channel) throws IOException {
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println("路由key= [ "+routingKey+" ]接收到的消息= [ "+msg +" ]");
        //Thread.sleep(5000);
        //发送ack给消息队列,收到消息了
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        //发送ack给消息队列,消息接收失败,并将消息放会队列
//        channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
    }

    @RabbitListener(queues = RabbitConstant.DEAD_QUEUE)
    public void deadQueueListener(Message message, Channel channel) throws IOException {
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println("路由key= [ "+routingKey+" ]接收到的消息= [ "+msg +" ]");
        //Thread.sleep(5000);
        //发送ack给消息队列,收到消息了
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
